﻿using Prism.Commands;
using Prism.Events;
using SoonCode.FocusTest.Events;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Prism.Mvvm;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;

namespace SoonCode.FocusTest.ViewModels
{
    internal class AnswerItemFViewModel : BindableBase
    {
        private string testType = "itemF";
        private ObservableCollection<FItemHelper> answerItems;
        private IEventAggregator aggregator;
        private EventInfo answerInfo;
        private int cutedown = 5;
        private int matrix = 2;
        private bool answerStart=false;
        private bool not_answerStart = true;
        private string tipDemoImg = "pack://application:,,,/Images/f_demo.png";
        public string TipDemoImg
        {
            get { return tipDemoImg; }
            set
            {
                tipDemoImg = value;
                RaisePropertyChanged();
            }
        }
        public bool Not_answerStart
        {
            get { return not_answerStart; }
            set
            {
                not_answerStart=value;
                RaisePropertyChanged();
            }
        }
        public bool AnswerStart
        {
            get { return answerStart; }
            set
            {
                answerStart = value;
                Not_answerStart=!answerStart;
                RaisePropertyChanged();
            }
        }
        public int Cutedown
        {
            get { return cutedown; }
            set
            {
                cutedown = value;
                RaisePropertyChanged();
            }
        }
        public int Matrix
        {
            get { return matrix; }
            set
            {
                matrix = value;
                RaisePropertyChanged();
            }
        }
        public ObservableCollection<FItemHelper> AnswerItems
        {
            get { return answerItems; }
            set
            {
                answerItems = value;
                RaisePropertyChanged();
            }
        }

        public AnswerItemFViewModel(IEventAggregator aggregator)
        {
            this.aggregator = aggregator;
            this.answerInfo = new EventInfo();
            answerInfo.Type = testType;
            AnswerCommand = new DelegateCommand<int?>(AnswerCommandHandler);
            SubmitAnwserCommand = new DelegateCommand(SubmitAnwserCommandHandler);
            this.aggregator.GetEvent<ChangeTestEvent>().Subscribe(p =>
            {
                AnswerStart = false;
            });
            this.aggregator.GetEvent<LevelUpdateEvent>().Subscribe(info =>
            {
                if (info.Type != testType) return;
                AnswerStart = true;
                AnswerItems = (ObservableCollection<FItemHelper>)info.Data["helper"];
                Matrix = (int)Math.Sqrt(AnswerItems.Count);

            });
            this.aggregator.GetEvent<QuestionChangeEvent>().Subscribe(info =>
            {
                if (info.Type != testType) return;
                Cutedown = (int)info.Data["cutedown"];
            });
            this.aggregator.GetEvent<TestLoadEvent>().Subscribe(OnLoaded);

        }

        private void OnLoaded(int p)
        {
            if (p != 5) return;
            AnswerStart = false;
            if(AnswerItems!=null)
                AnswerItems.Clear();
            int age=GlobalModel.Instance.MemberInfo.age;
            TipDemoImg=age>=5?"pack://application:,,,/Images/f_demo.png":"pack://application:,,,/Images/f_demo2.png";
        }

        public ICommand AnswerCommand { get; set; }
        public ICommand SubmitAnwserCommand { get; set; }

        private void SubmitAnwserCommandHandler()
        {
            if (Cutedown != 0) return;
            answerInfo.Data["submit"] = true;
            this.aggregator.GetEvent<StudentAnswerEvent>().Publish(answerInfo);
        }
        private void AnswerCommandHandler(int? p)
        {
            if (Cutedown != 0) return;
            SoundPlayer.PlayButton();
            answerInfo.Data.Remove("submit");
            if (AnswerItems == null) AnswerItems = new ObservableCollection<FItemHelper>();
            answerInfo.Data["index"] = p;
            this.aggregator.GetEvent<StudentAnswerEvent>().Publish(answerInfo);
        }


    }
}
